package com.fenghuajueli.module_user.model;

import android.text.TextUtils;
import android.util.Log;

import androidx.lifecycle.LifecycleOwner;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.fenghuajueli.lib_data.entity.goods.SignStatusEntity;
import com.fenghuajueli.lib_data.entity.goods.ZfbSubscribeVipEntity;
import com.fenghuajueli.libbasecoreui.constants.SwitchConfig;
import com.fenghuajueli.libbasecoreui.listener.BaseCallBackListener;
import com.fenghuajueli.libbasecoreui.listener.TokenErrorBackListener;
import com.fenghuajueli.libbasecoreui.user.UserInfoUtils;
import com.fenghuajueli.lib_data.entity.UserInfoEntity;
import com.fenghuajueli.lib_data.entity.goods.GoodsData;
import com.fenghuajueli.lib_data.entity.goods.WeChatPayInfoEntity;
import com.fenghuajueli.lib_data.entity.goods.ZfbPayInfoEntity;
import com.fenghuajueli.lib_net.common.DefaultObserver;
import com.fenghuajueli.lib_net.common.RetrofitUtils;
import com.fenghuajueli.lib_net.common.TokenObserver;
import com.fenghuajueli.module_user.constants.NetConstants;
import com.fenghuajueli.module_user.net.ApiService;
import com.fenghuajueli.module_user.utils.AesUtils;

import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Retrofit;

/**
 * 将所有的接口请求做成工具类 直接调用,每个接口都需要传入当前界面的生面周期监听管理类
 */
public class RequestInfoModel {

    public static String APP_ID = "";

    //获取商品接口传递的是否包含订阅类商品的值
    private static final int NO_CONTAIN_SUBSCRIBE = 0;
    private static final int CONTAIN_SUBSCRIBE = 1;

    private static final int PAGE = 1;
    private static final int PAGE_SIZE = 30;

    //获取商品的上架参数状态值，默认获取上架的商品
    private static final int PUT_ON_STATUS = 1;

    public static ApiService apiService;

    static {
        createApiServices();
    }

    public static Retrofit.Builder getDefault() {
        return AppUtils.isAppDebug() ?
                RetrofitUtils.getRetrofitBuilder(NetConstants.BASE_DEBUG_URL)
                : RetrofitUtils.getRetrofitBuilder(NetConstants.BASE_RELEASE_URL);
    }

    private static void createApiServices() {
        if (apiService == null) {
            apiService = getDefault().build().create(ApiService.class);
        }
    }

    public static void login(LifecycleOwner owner, String phone, String passWord, final BaseCallBackListener<UserInfoEntity> baseCallBackListener) {
        if (TextUtils.isEmpty(phone)) {
            ToastUtils.showShort("请输入手机号");
            return;
        }
        if (!phone.startsWith("1") || phone.length() != 11) {
            ToastUtils.showShort("请输入正确的手机号");
            return;
        }
        if (TextUtils.isEmpty(passWord)) {
            ToastUtils.showShort("请输入密码");
            return;
        }
        String account = AesUtils.encrypt(phone, APP_ID);
        String enPassword = AesUtils.encrypt(passWord, APP_ID);
        if (TextUtils.isEmpty(account) || TextUtils.isEmpty(enPassword)) {
            ToastUtils.showShort("请检查用户名和密码！");
            return;
        }
        //执行登录
        baseCallBackListener.onHandlerStart();
        createApiServices();
        //执行登录
        Map<String, String> par = new HashMap<>();
        par.put("account", account);
        par.put("password", enPassword);
        par.put("is_encrypt", "1");
        par.put("app_id", APP_ID);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), GsonUtils.toJson(par));
        apiService.login(requestBody)
                .with(owner)
                .subscribe(new DefaultObserver<UserInfoEntity>() {
                    @Override
                    public void onError(String errorMsg) {
                        baseCallBackListener.onFailed(errorMsg);
                    }

                    @Override
                    public void onSuccess(UserInfoEntity data) {
                        UserInfoUtils.getInstance().setUserInfoEntity(data);
                        baseCallBackListener.onSuccess(data);
                    }
                });
    }


    public static void register(LifecycleOwner owner, String phone, String passWord,
                                String confirmPhone, String confirmPwd,
                                final BaseCallBackListener<UserInfoEntity> baseCallBackListener) {
        if (TextUtils.isEmpty(phone)) {
            ToastUtils.showShort("请输入手机号");
            return;
        }
        if (!phone.startsWith("1") || phone.length() != 11) {
            ToastUtils.showShort("请输入正确的手机号");
            return;
        }
        if (TextUtils.isEmpty(passWord)) {
            ToastUtils.showShort("请输入密码");
            return;
        }
        if (TextUtils.isEmpty(confirmPhone)) {
            ToastUtils.showShort("请输入手机号");
            return;
        }
        if (!confirmPhone.startsWith("1") || confirmPhone.length() != 11) {
            ToastUtils.showShort("请输入正确的手机号");
            return;
        }
        if (TextUtils.isEmpty(confirmPwd)) {
            ToastUtils.showShort("请输入密码");
            return;
        }
        if (passWord.length() < 8) {
            ToastUtils.showShort("密码必须大于8位");
            return;
        }
        if (confirmPwd.length() < 8) {
            ToastUtils.showShort("密码必须大于8位");
            return;
        }
        if (!passWord.equals(confirmPwd)) {
            ToastUtils.showShort("两次输入的密码不一致");
            return;
        }
        if (!phone.equals(confirmPhone)) {
            ToastUtils.showShort("两次输入的手机号不一致");
            return;
        }
        //执行注册
        String account = AesUtils.encrypt(phone, APP_ID);
        String enPassword = AesUtils.encrypt(passWord, APP_ID);
        if (TextUtils.isEmpty(account) || TextUtils.isEmpty(enPassword)) {
            ToastUtils.showShort("请检查用户名和密码！");
            return;
        }
        createApiServices();
        baseCallBackListener.onHandlerStart();
        Map<String, String> par = new HashMap<>();
        par.put("type", "2");
        par.put("phone", account);
        par.put("user_name", account);
        par.put("confirm_phone", account);
        par.put("confirm_password", enPassword);
        par.put("password", enPassword);
        par.put("is_encrypt", "1");
        par.put("app_id", APP_ID);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), GsonUtils.toJson(par));
        apiService.register(requestBody)
                .with(owner)
                .subscribe(new DefaultObserver<UserInfoEntity>() {
                    @Override
                    public void onError(String errorMsg) {
                        baseCallBackListener.onFailed(errorMsg);
                    }

                    @Override
                    public void onSuccess(UserInfoEntity data) {
                        UserInfoUtils.getInstance().setUserInfoEntity(data);
                        baseCallBackListener.onSuccess(data);
                    }
                });
    }


    public static void editPassword(LifecycleOwner owner, String phone,
                                    String passWord, String confirmPwd, String code,
                                    final BaseCallBackListener<UserInfoEntity> baseCallBackListener) {
        if (TextUtils.isEmpty(phone)) {
            ToastUtils.showShort("请输入手机号");
            return;
        }
        if (!phone.startsWith("1") || phone.length() != 11) {
            ToastUtils.showShort("请输入正确的手机号");
            return;
        }
        if (TextUtils.isEmpty(passWord)) {
            ToastUtils.showShort("请输入密码");
            return;
        }
        if (TextUtils.isEmpty(code)) {
            ToastUtils.showShort("请输入验证码");
            return;
        }
        if (TextUtils.isEmpty(confirmPwd)) {
            ToastUtils.showShort("请输入密码");
            return;
        }
        if (!passWord.equals(confirmPwd)) {
            ToastUtils.showShort("两次输入的密码不一样！");
            return;
        }
        if (passWord.length() < 8) {
            ToastUtils.showShort("密码必须大于8位");
            return;
        }
        String account = AesUtils.encrypt(phone, APP_ID);
        String enPassword = AesUtils.encrypt(passWord, APP_ID);
        if (TextUtils.isEmpty(account) || TextUtils.isEmpty(enPassword)) {
            ToastUtils.showShort("请检查用户名和密码！");
            return;
        }
        //执行注册
        baseCallBackListener.onHandlerStart();
        createApiServices();
        Map<String, String> par = new HashMap<>();
        par.put("phone", account);
        par.put("code", code);
        par.put("password", enPassword);
        par.put("is_encrypt", "1");
        par.put("app_id", APP_ID);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), GsonUtils.toJson(par));
        apiService.resetPassword(requestBody)
                .with(owner)
                .subscribe(new DefaultObserver<UserInfoEntity>() {
                    @Override
                    public void onError(String errorMsg) {
                        baseCallBackListener.onFailed(errorMsg);
                    }

                    @Override
                    public void onSuccess(UserInfoEntity data) {
                        UserInfoUtils.getInstance().setUserInfoEntity(data);
                        baseCallBackListener.onSuccess(data);
                    }
                });
    }

    public static void getVerificationCode(LifecycleOwner owner, String phone, final BaseCallBackListener<String> baseCallBackListener) {
        if (TextUtils.isEmpty(phone)) {
            ToastUtils.showShort("请输入手机号");
            return;
        }
        if (!phone.startsWith("1") || phone.length() != 11) {
            ToastUtils.showShort("请输入正确的手机号");
            return;
        }
        String account = AesUtils.encrypt(phone, APP_ID);
        if (TextUtils.isEmpty(account)) {
            ToastUtils.showShort("请检查用户名！");
            return;
        }
        //执行注册
        baseCallBackListener.onHandlerStart();
        createApiServices();
        Map<String, String> par = new HashMap<>();
        par.put("phone", account);
        par.put("is_encrypt", "1");
        par.put("app_id", APP_ID);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), GsonUtils.toJson(par));
        apiService.getVerificationCode(requestBody)
                .with(owner)
                .subscribe(new DefaultObserver<String>() {
                    @Override
                    public void onError(String errorMsg) {
                        baseCallBackListener.onFailed(errorMsg);
                    }

                    @Override
                    public void onSuccess(String data) {
                        baseCallBackListener.onSuccess(data);
                    }
                });
    }


    /**
     * 获取商品列表，默认传递的自营渠道,且不获取订阅类商品
     *
     * @param owner
     * @param token
     * @param baseCallBackListener
     */
    public static void getGoodsList(LifecycleOwner owner, String token, final TokenErrorBackListener<GoodsData> baseCallBackListener) {
        if (TextUtils.isEmpty(token)) {
            ToastUtils.showShort("token为空！");
            return;
        }
        baseCallBackListener.onHandlerStart();
        createApiServices();
        Map<String, String> par = new HashMap<>();
        par.put("token", token);
        apiService.getGoodsList(par, "1", NO_CONTAIN_SUBSCRIBE, PAGE, PAGE_SIZE, PUT_ON_STATUS)
                .with(owner)
                .subscribe(new TokenObserver<GoodsData>() {
                    @Override
                    public void onError(String errorMsg) {
                        baseCallBackListener.onFailed(errorMsg);
                    }

                    @Override
                    public void onTokenError(int code, String message) {
                        baseCallBackListener.tokenError(code, message);
                    }

                    @Override
                    public void onSuccess(GoodsData data) {
                        baseCallBackListener.onSuccess(data);
                    }
                });
    }

    /**
     * 根据渠道号获取商品信息，并且根据isGetSubscribe参数来决定是否获取订阅类商品
     *
     * @param owner
     * @param token
     * @param channel              渠道号详见接口文档
     * @param isGetSubscribe       是否获取订阅类商品
     * @param baseCallBackListener
     */
    public static void getGoodsList(LifecycleOwner owner, String token, String channel, boolean isGetSubscribe, final TokenErrorBackListener<GoodsData> baseCallBackListener) {
        if (TextUtils.isEmpty(token)) {
            ToastUtils.showShort("token为空！");
            return;
        }
        baseCallBackListener.onHandlerStart();
        createApiServices();
        Map<String, String> par = new HashMap<>();
        par.put("token", token);
        apiService.getGoodsList(par, channel, isGetSubscribe ? CONTAIN_SUBSCRIBE : NO_CONTAIN_SUBSCRIBE, PAGE, PAGE_SIZE, PUT_ON_STATUS)
                .with(owner)
                .subscribe(new TokenObserver<GoodsData>() {
                    @Override
                    public void onError(String errorMsg) {
                        baseCallBackListener.onFailed(errorMsg);
                    }

                    @Override
                    public void onTokenError(int code, String message) {
                        baseCallBackListener.tokenError(code, message);
                    }

                    @Override
                    public void onSuccess(GoodsData data) {
                        baseCallBackListener.onSuccess(data);
                    }
                });
    }


    public static void getWechatOrder(LifecycleOwner owner, String token, String channel, String goodId, final TokenErrorBackListener<WeChatPayInfoEntity> baseCallBackListener) {
        baseCallBackListener.onHandlerStart();
        createApiServices();
        Map<String, String> header = new HashMap<>();
        header.put("token", token);
        Map<String, String> par = new HashMap<>();
        par.put("channel", channel);
        par.put("market_channel", String.valueOf(getMarketChannel()));
        par.put("goods_id", goodId);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), GsonUtils.toJson(par));
        apiService.getWechatOrder(header, requestBody)
                .with(owner)
                .subscribe(new TokenObserver<WeChatPayInfoEntity>() {
                    @Override
                    public void onError(String errorMsg) {
                        baseCallBackListener.onFailed(errorMsg);
                    }

                    @Override
                    public void onTokenError(int code, String message) {
                        baseCallBackListener.tokenError(code, message);
                    }

                    @Override
                    public void onSuccess(WeChatPayInfoEntity data) {
                        baseCallBackListener.onSuccess(data);
                    }
                });
    }

    private static int getMarketChannel() {
        if (SwitchConfig.isHuaWeiChannel()) {
            return 8;
        }
        if (SwitchConfig.isXiaoMiChannel()) {
            return 9;
        }
        if (SwitchConfig.isOppoChannel()) {
            return 10;
        }
        if (SwitchConfig.isVivoChannel()) {
            return 11;
        }
        return 14;//默认传递应用宝的渠道值
    }


    public static void getZfbOrder(LifecycleOwner owner, String token, String channel, String goodId, final TokenErrorBackListener<ZfbPayInfoEntity> baseCallBackListener) {
        baseCallBackListener.onHandlerStart();
        createApiServices();
        Map<String, String> header = new HashMap<>();
        header.put("token", token);
        Map<String, String> par = new HashMap<>();
        par.put("channel", channel);
        par.put("market_channel", String.valueOf(getMarketChannel()));
        par.put("goods_id", goodId);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), GsonUtils.toJson(par));
        apiService.getZfbOrder(header, requestBody)
                .with(owner)
                .subscribe(new TokenObserver<ZfbPayInfoEntity>() {
                    @Override
                    public void onError(String errorMsg) {
                        baseCallBackListener.onFailed(errorMsg);
                    }

                    @Override
                    public void onTokenError(int code, String message) {
                        baseCallBackListener.tokenError(code, message);
                    }

                    @Override
                    public void onSuccess(ZfbPayInfoEntity data) {
                        baseCallBackListener.onSuccess(data);
                    }
                });
    }


    public static void getUserInfo(LifecycleOwner owner, String token, final TokenErrorBackListener<UserInfoEntity.UserInfoBean> baseCallBackListener) {
        baseCallBackListener.onHandlerStart();
        createApiServices();
        Map<String, String> header = new HashMap<>();
        header.put("token", token);
        apiService.getUserInfo(header)
                .with(owner)
                .subscribe(new TokenObserver<UserInfoEntity.UserInfoBean>() {
                    @Override
                    public void onError(String errorMsg) {
                        baseCallBackListener.onFailed(errorMsg);
                    }

                    @Override
                    public void onTokenError(int code, String message) {
                        baseCallBackListener.tokenError(code, message);
                    }

                    @Override
                    public void onSuccess(UserInfoEntity.UserInfoBean data) {
                        UserInfoUtils.getInstance().getUserInfoEntity().setUserInfo(data);
                        UserInfoUtils.getInstance().saveUserInfoToLocal(UserInfoUtils.getInstance().getUserInfoEntity());
                        baseCallBackListener.onSuccess(data);
                    }
                });

    }


    public static void getUserInfoNoLifecycle(String token, final TokenErrorBackListener<UserInfoEntity.UserInfoBean> baseCallBackListener) {
        baseCallBackListener.onHandlerStart();
        createApiServices();
        Map<String, String> header = new HashMap<>();
        header.put("token", token);
        apiService.getUserInfo(header)
                .subscribe(new TokenObserver<UserInfoEntity.UserInfoBean>() {
                    @Override
                    public void onError(String errorMsg) {
                        baseCallBackListener.onFailed(errorMsg);
                    }

                    @Override
                    public void onTokenError(int code, String message) {
                        baseCallBackListener.tokenError(code, message);
                    }

                    @Override
                    public void onSuccess(UserInfoEntity.UserInfoBean data) {
                        UserInfoUtils.getInstance().getUserInfoEntity().setUserInfo(data);
                        UserInfoUtils.getInstance().saveUserInfoToLocal(UserInfoUtils.getInstance().getUserInfoEntity());
                        baseCallBackListener.onSuccess(data);
                    }
                });

    }


    public static void activePhone(LifecycleOwner owner, String token, String phone, String code, final TokenErrorBackListener<UserInfoEntity.UserInfoBean> baseCallBackListener) {
        String account = AesUtils.encrypt(phone, APP_ID);
        if (TextUtils.isEmpty(account)) {
            ToastUtils.showShort("请检查用户名！");
            return;
        }
        baseCallBackListener.onHandlerStart();
        createApiServices();
        Map<String, String> header = new HashMap<>();
        header.put("token", token);
        Map<String, String> par = new HashMap<>();
        par.put("phone", account);
        par.put("code", code);
        par.put("is_encrypt", "1");
        par.put("app_id", APP_ID);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), GsonUtils.toJson(par));
        apiService.activePhone(header, requestBody)
                .with(owner)
                .subscribe(new TokenObserver<UserInfoEntity.UserInfoBean>() {
                    @Override
                    public void onError(String errorMsg) {
                        baseCallBackListener.onFailed(errorMsg);
                    }

                    @Override
                    public void onTokenError(int code, String message) {
                        baseCallBackListener.tokenError(code, message);
                    }

                    @Override
                    public void onSuccess(UserInfoEntity.UserInfoBean data) {
                        baseCallBackListener.onSuccess(data);
                    }
                });
    }

    public static void deleteUser(LifecycleOwner owner, String token, int userId, String phone, String code, final BaseCallBackListener<String> baseCallBackListener) {
        if (TextUtils.isEmpty(token)) {
            ToastUtils.showShort("token为空！");
            return;
        }
        if (TextUtils.isEmpty(phone)) {
            ToastUtils.showShort("请输入手机号");
            return;
        }
        if (!phone.startsWith("1") || phone.length() != 11) {
            ToastUtils.showShort("请输入正确的手机号");
            return;
        }
        if (TextUtils.isEmpty(code)) {
            ToastUtils.showShort("请输入验证码");
            return;
        }
        String account = AesUtils.encrypt(phone, APP_ID);
        //为了兼容 需要传一个默认的密码
        String enPassword = AesUtils.encrypt("123456", APP_ID);
        if (TextUtils.isEmpty(account)) {
            ToastUtils.showShort("请检查用户名！");
            return;
        }
        baseCallBackListener.onHandlerStart();
        createApiServices();
        apiService.deleteRegisterUser(userId, token, account, enPassword, code, "1", APP_ID)
                .with(owner)
                .subscribe(new DefaultObserver<String>() {
                    @Override
                    public void onError(String errorMsg) {
                        baseCallBackListener.onFailed(errorMsg);
                    }

                    @Override
                    public void onSuccess(String data) {
                        baseCallBackListener.onSuccess(data);
                    }
                });
    }


    /**
     * 第三方微信登录
     *
     * @param unionId
     * @param accessToken
     * @param baseCallBackListener
     */
    public static void thirdWechatLogin(LifecycleOwner owner, String unionId, String accessToken, final BaseCallBackListener<UserInfoEntity> baseCallBackListener) {
        //执行登录
        baseCallBackListener.onHandlerStart();
        createApiServices();
        //执行登录
        Map<String, String> par = new HashMap<>();
        par.put("app_id", APP_ID);
        par.put("type", "4");
        par.put("third_type", "1");
        par.put("account", unionId);
        par.put("third_token", accessToken);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), GsonUtils.toJson(par));
        apiService.thirdLogin(requestBody)
                .with(owner)
                .subscribe(new DefaultObserver<UserInfoEntity>() {
                    @Override
                    public void onError(String errorMsg) {
                        baseCallBackListener.onFailed(errorMsg);
                    }

                    @Override
                    public void onSuccess(UserInfoEntity data) {
                        UserInfoUtils.getInstance().setUserInfoEntity(data);
                        baseCallBackListener.onSuccess(data);
                    }
                });
    }

    /**
     * 检测账号是否存在
     *
     * @param baseCallBackListener
     */
    public static void checkAccount(LifecycleOwner owner, String account, final BaseCallBackListener<Boolean> baseCallBackListener) {
        if (TextUtils.isEmpty(account)) {
            ToastUtils.showShort("请输入手机号");
            return;
        }
        if (!account.startsWith("1") || account.length() != 11) {
            ToastUtils.showShort("请输入正确的手机号");
            return;
        }
        //执行登录
        baseCallBackListener.onHandlerStart();
        createApiServices();
        //执行登录
        Map<String, String> par = new HashMap<>();
        par.put("account", account);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), GsonUtils.toJson(par));
        apiService.checkAccount(requestBody)
                .with(owner)
                .subscribe(new DefaultObserver<Boolean>() {
                    @Override
                    public void onError(String errorMsg) {
                        baseCallBackListener.onFailed(errorMsg);
                    }

                    @Override
                    public void onSuccess(Boolean data) {
                        baseCallBackListener.onSuccess(data);
                    }
                });
    }

    /**
     * 获取支付宝订阅掉起相关参数
     *
     * @param owner
     * @param token                用户登录后的 token
     * @param goodsId              商品的id
     * @param baseCallBackListener
     */
    public static void getZfbSubscribeVipParams(LifecycleOwner owner, String token, String goodsId, final TokenErrorBackListener<ZfbSubscribeVipEntity> baseCallBackListener) {
        if (TextUtils.isEmpty(token)) {
            ToastUtils.showShort("Token无效！");
            return;
        }
        if (TextUtils.isEmpty(goodsId)) {
            ToastUtils.showShort("商品id不能为空！");
            return;
        }
        //执行登录
        baseCallBackListener.onHandlerStart();
        createApiServices();
        //执行登录
        Map<String, String> par = new HashMap<>();
        par.put("channel", "2");
        par.put("goods_id", goodsId);
        par.put("app_id", APP_ID);
        par.put("app_version", AppUtils.getAppVersionName() + "");
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), GsonUtils.toJson(par));
        apiService.preSubscribeOrder(token, requestBody)
                .with(owner)
                .subscribe(new TokenObserver<ZfbSubscribeVipEntity>() {
                    @Override
                    public void onError(String errorMsg) {
                        baseCallBackListener.onFailed(errorMsg);
                    }

                    @Override
                    public void onTokenError(int code, String message) {
                        baseCallBackListener.tokenError(code, message);
                    }

                    @Override
                    public void onSuccess(ZfbSubscribeVipEntity data) {
                        baseCallBackListener.onSuccess(data);
                    }
                });
    }


    /**
     * 获取支付宝订阅的状态
     *
     * @param owner
     * @param baseCallBackListener
     */
    public static void checkZfbSubscribeSignStatus(LifecycleOwner owner, String token, final TokenErrorBackListener<SignStatusEntity> baseCallBackListener) {
        if (TextUtils.isEmpty(token)) {
            ToastUtils.showShort("Token无效！");
            return;
        }
        //执行登录
        baseCallBackListener.onHandlerStart();
        createApiServices();
        apiService.checkSubscribeSignStatus(token, "2")
                .with(owner)
                .subscribe(new TokenObserver<SignStatusEntity>() {
                    @Override
                    public void onError(String errorMsg) {
                        baseCallBackListener.onFailed(errorMsg);
                    }

                    @Override
                    public void onTokenError(int code, String message) {
                        baseCallBackListener.tokenError(code, message);
                    }

                    @Override
                    public void onSuccess(SignStatusEntity data) {
                        baseCallBackListener.onSuccess(data);
                    }
                });
    }


    public static void oneKeyLogin(LifecycleOwner owner, String accessToken, final BaseCallBackListener<UserInfoEntity> baseCallBackListener) {
        if (TextUtils.isEmpty(accessToken)) {
            ToastUtils.showShort("accessToken为空，登录失败！");
            return;
        }
        //执行登录
        baseCallBackListener.onHandlerStart();
        createApiServices();
        //执行登录
        Map<String, String> par = new HashMap<>();
        par.put("type", "5");
        par.put("access_token", accessToken);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), GsonUtils.toJson(par));
        apiService.login(requestBody)
                .with(owner)
                .subscribe(new DefaultObserver<UserInfoEntity>() {
                    @Override
                    public void onError(String errorMsg) {
                        baseCallBackListener.onFailed(errorMsg);
                    }

                    @Override
                    public void onSuccess(UserInfoEntity data) {
                        UserInfoUtils.getInstance().setUserInfoEntity(data);
                        baseCallBackListener.onSuccess(data);
                    }
                });
    }


    /**
     * 解绑第三方账户  必须得是第三方账户绑定了手机后才能解绑 ，ui界面上处理如果第三方登录的账号没有绑定手机，
     * 则就不显示解绑的按钮
     *
     * @param owner
     * @param token
     * @param baseCallBackListener
     */
    public static void unbindThirdAccount(LifecycleOwner owner, String token, final TokenErrorBackListener<String> baseCallBackListener) {
        if (TextUtils.isEmpty(token)) {
            ToastUtils.showShort("Token无效！");
            return;
        }
        //执行登录
        baseCallBackListener.onHandlerStart();
        createApiServices();
        apiService.unbindThirdAccount(token)
                .with(owner)
                .subscribe(new TokenObserver<String>() {
                    @Override
                    public void onError(String errorMsg) {
                        baseCallBackListener.onFailed(errorMsg);
                    }

                    @Override
                    public void onTokenError(int code, String message) {
                        baseCallBackListener.tokenError(code, message);
                    }

                    @Override
                    public void onSuccess(String data) {
                        baseCallBackListener.onSuccess(data);
                    }
                });
    }


    /**
     * 第三方账号注销账号 主要是微信
     *
     * @param owner
     * @param token
     * @param baseCallBackListener
     */
    public static void logoutWxAccount(LifecycleOwner owner, String wxUid, String wxToken, String token, final TokenErrorBackListener<String> baseCallBackListener) {
        if (TextUtils.isEmpty(token)) {
            ToastUtils.showShort("Token无效！");
            return;
        }
        baseCallBackListener.onHandlerStart();
        createApiServices();
        apiService.logoutThirdAccount(token, "1", wxUid, wxToken)
                .with(owner)
                .subscribe(new TokenObserver<String>() {
                    @Override
                    public void onError(String errorMsg) {
                        baseCallBackListener.onFailed(errorMsg);
                    }

                    @Override
                    public void onTokenError(int code, String message) {
                        baseCallBackListener.tokenError(code, message);
                    }

                    @Override
                    public void onSuccess(String data) {
                        baseCallBackListener.onSuccess(data);
                    }
                });
    }


    /**
     * 第三方账号绑定手机的处理
     *
     * @param owner
     * @param token
     * @param phone
     * @param code
     * @param baseCallBackListener
     */
    public static void bindPhone(LifecycleOwner owner, String token, String phone, String code, String password, final TokenErrorBackListener<UserInfoEntity.UserInfoBean.VipInfoBean> baseCallBackListener) {
        String account = AesUtils.encrypt(phone, APP_ID);
        String aesPwd = AesUtils.encrypt(password, APP_ID);
        if (TextUtils.isEmpty(account)) {
            ToastUtils.showShort("请检查用户名！");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            ToastUtils.showShort("请输入密码");
            return;
        }
        if (password.length() < 8) {
            ToastUtils.showShort("密码必须大于8位");
            return;
        }
        baseCallBackListener.onHandlerStart();
        createApiServices();
        Map<String, String> header = new HashMap<>();
        header.put("token", token);
        Map<String, String> par = new HashMap<>();
        par.put("phone", account);
        par.put("code", code);
        par.put("is_encrypt", "1");
        par.put("app_id", APP_ID);
        par.put("password", aesPwd);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), GsonUtils.toJson(par));
        apiService.thirdBindPhone(header, requestBody)
                .with(owner)
                .subscribe(new TokenObserver<UserInfoEntity.UserInfoBean.VipInfoBean>() {
                    @Override
                    public void onError(String errorMsg) {
                        baseCallBackListener.onFailed(errorMsg);
                    }

                    @Override
                    public void onTokenError(int code, String message) {
                        baseCallBackListener.tokenError(code, message);
                    }

                    @Override
                    public void onSuccess(UserInfoEntity.UserInfoBean.VipInfoBean data) {
                        LogUtils.d("当前手机号绑定成功-------》");
                        LogUtils.json(data);
                        baseCallBackListener.onSuccess(data);
                    }
                });
    }
}
